﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DatabaseExtension;

namespace System.Data.Common.QueryBuilder
{
    public class QJoin
    {
        public enum JoinType
        {
            INNER_JOIN,
            LEFT_JOIN,
            RIGHT_JOIN,
            FULL_OUTER_JOIN,
            NATURAL_JOIN
        }
        public DbQuerySelect EndJoin;
        public String TableName;
        public JoinType Type;
        private List<String> Joincomparisation;
        private Boolean ComparerUsed;

        public QJoin Or
        {
            get
            {
                this.ComparerUsed = true;
                this.Joincomparisation.Add("\r\n\t\tOR\t");
                return this;
            }
        }

        public QJoin And
        {
            get
            {
                this.ComparerUsed = true;
                this.Joincomparisation.Add("\r\n\t\tAND\t");
                return this;
            }
        }

        public QJoin(DbQuerySelect parent, String table, JoinType type)
        {
            this.EndJoin = parent;
            this.TableName = table;
            this.Type = type;
            this.Joincomparisation = new List<String>();
            this.ComparerUsed = true;
        }

        public QJoin On(String compar)
        {
            if (this.ComparerUsed == false)
                throw new ArgumentException("Cannot genereate concationation, if operators 'and' or 'or' are not in use.");
            this.Joincomparisation.Add(compar);
            this.ComparerUsed = false;
            return this;
        }

        public override string ToString()
        {
            return String.Concat(
                this.Type.EnumToString(), " ", this.TableName, 
                    Environment.NewLine, "\t", "ON",
                        String.Concat(Environment.NewLine, "\t\t\t"), String.Join(String.Empty, this.Joincomparisation)
                );
        }
    }
}
